package io.github.jsp.example;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 服务监控定时任务使用示例
 * 
 * 这个示例演示了如何使用ServiceMonitoringJobHandler来监控各种服务的健康状态
 */
@Component
public class ServiceMonitoringExample implements ApplicationRunner {
    
    private static final Logger logger = LoggerFactory.getLogger(ServiceMonitoringExample.class);
    
    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("=== 服务监控定时任务使用示例 ===");
        
        printBasicExample();
        printAdvancedExample();
        printProductionExample();
    }
    
    /**
     * 基础监控示例 - 监控本地服务
     */
    private void printBasicExample() {
        logger.info("\n1. 基础TCP端口监控示例:");
        logger.info("任务描述: 本地服务端口监控");
        logger.info("Cron表达式: 0/30 * * * * ? (每30秒执行)"); 
        logger.info("执行类名: ServiceMonitoringJobHandler");
        logger.info("任务参数JSON:");
        
        String basicJson = """
            {
              "services": [
                {
                  "name": "PostgreSQL数据库",
                  "host": "localhost",
                  "port": 5432,
                  "protocol": "tcp",
                  "timeoutMs": 3000,
                  "alertEnabled": true,
                  "alertEmails": "dba@example.com"
                },
                {
                  "name": "应用服务",
                  "host": "localhost", 
                  "port": 8085,
                  "protocol": "tcp",
                  "timeoutMs": 2000,
                  "alertEnabled": true,
                  "alertEmails": "admin@example.com"
                }
              ]
            }
            """;
        
        logger.info(basicJson);
    }
    
    /**
     * 进阶监控示例 - HTTP健康检查
     */
    private void printAdvancedExample() {
        logger.info("\n2. HTTP服务健康检查示例:");
        logger.info("任务描述: Web服务健康状态监控");
        logger.info("Cron表达式: 0/10 * * * * ? (每10秒执行)");
        logger.info("执行类名: ServiceMonitoringJobHandler");
        logger.info("任务参数JSON:");
        
        String advancedJson = """
            {
              "services": [
                {
                  "name": "本地Web服务",
                  "host": "localhost",
                  "port": 8085,
                  "protocol": "http",
                  "healthCheckUrl": "http://localhost:8085/api/system/info",
                  "expectedResponse": "200",
                  "timeoutMs": 5000,
                  "alertEnabled": true,
                  "alertEmails": "dev@example.com"
                },
                {
                  "name": "百度搜索",
                  "host": "www.baidu.com",
                  "port": 443,
                  "protocol": "https", 
                  "healthCheckUrl": "https://www.baidu.com",
                  "expectedResponse": "百度",
                  "timeoutMs": 8000,
                  "alertEnabled": false
                }
              ]
            }
            """;
            
        logger.info(advancedJson);
    }
    
    /**
     * 生产环境监控示例 - 完整的微服务监控
     */
    private void printProductionExample() {
        logger.info("\n3. 生产环境微服务监控示例:");
        logger.info("任务描述: 微服务集群健康监控");
        logger.info("Cron表达式: 0 * * * * ? (每分钟执行)");
        logger.info("执行类名: ServiceMonitoringJobHandler"); 
        logger.info("任务参数JSON:");
        
        String productionJson = """
            {
              "services": [
                {
                  "name": "用户服务",
                  "host": "user-service",
                  "port": 8080,
                  "protocol": "http",
                  "healthCheckUrl": "http://user-service:8080/actuator/health",
                  "expectedResponse": "UP",
                  "timeoutMs": 5000,
                  "alertEnabled": true,
                  "alertEmails": "backend-team@company.com"
                },
                {
                  "name": "订单服务",
                  "host": "order-service",
                  "port": 8080,
                  "protocol": "http",
                  "healthCheckUrl": "http://order-service:8080/health", 
                  "expectedResponse": "healthy",
                  "timeoutMs": 6000,
                  "alertEnabled": true,
                  "alertEmails": "order-team@company.com"
                },
                {
                  "name": "支付服务",
                  "host": "payment-service",
                  "port": 8080,
                  "protocol": "http",
                  "healthCheckUrl": "http://payment-service:8080/status",
                  "expectedResponse": "ok",
                  "timeoutMs": 10000,
                  "alertEnabled": true,
                  "alertEmails": "payment-team@company.com,ops@company.com"
                },
                {
                  "name": "Redis缓存",
                  "host": "redis-cluster",
                  "port": 6379,
                  "protocol": "tcp",
                  "timeoutMs": 2000,
                  "alertEnabled": true,
                  "alertEmails": "ops@company.com"
                },
                {
                  "name": "MySQL主库",
                  "host": "mysql-master",
                  "port": 3306,
                  "protocol": "tcp",
                  "timeoutMs": 3000,
                  "alertEnabled": true,
                  "alertEmails": "dba@company.com,ops@company.com"
                },
                {
                  "name": "Elasticsearch",
                  "host": "elasticsearch",
                  "port": 9200,
                  "protocol": "http",
                  "healthCheckUrl": "http://elasticsearch:9200/_cluster/health",
                  "expectedResponse": "green",
                  "timeoutMs": 8000,
                  "alertEnabled": true,
                  "alertEmails": "data-team@company.com"
                },
                {
                  "name": "RabbitMQ",
                  "host": "rabbitmq",
                  "port": 5672,
                  "protocol": "tcp",
                  "timeoutMs": 4000,
                  "alertEnabled": true,
                  "alertEmails": "mq-admin@company.com"
                }
              ]
            }
            """;
            
        logger.info(productionJson);
    }
}